package com.zktr.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zktr.bean.CategoryBean;
import com.zktr.pojo.Brand;
import com.zktr.pojo.Category;
import com.zktr.pojo.Goods;
import com.zktr.pojo.PageBean;
import com.zktr.service.BrandService;
import com.zktr.service.CategoryService;
import com.zktr.service.GoodsService;


@WebServlet("/CategoryServlet")
public class CategoryServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    private CategoryService service=new CategoryService();
    private GoodsService gService=new GoodsService();
    private BrandService bService=new BrandService();
	
	/**
	 * 搜索框模糊查询
	 * 局部刷新
	 */
	public void queryLike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String name = request.getParameter("value");
		List<Category> namelist = service.queryLike(name);
		String jsonstring=JSON.toJSONString(namelist);
		JSONArray parseArray = JSONArray.parseArray(jsonstring);
		out.println(parseArray);
	}
	
	/**
	 * 分页查询所有类别
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void quaryAllCaregory(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		int pc = getPc(req);
		int ps = 15;
		PageBean<CategoryBean> pageBean = service.findAllCategory(pc, ps);
		pageBean.setUrl(getUrl(req));
		req.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		req.getSession().setAttribute("pb",pageBean);
		out.println("0");
	}
	/**
	 * 查询所有分类名称
	 * @param req
	 * @param res
	 * @throws IOException
	 */

	private int getPc(HttpServletRequest req) {
		String value = req.getParameter("pc");
		if(value == null || value.trim().isEmpty()) {
			return 1;
		}
		try{
			return Integer.parseInt(value);
		}catch(Exception e){
			return  1;
		}
	}
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();
		String servletPath = request.getServletPath();
		String queryString = request.getQueryString();
		if(queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		return contextPath + servletPath + "?" + queryString;
	}
	/**
	 * 直接从session获取封装三级分类的map集合
	 * 根据所传的id值得到第二个首页的分类重新撞到map集合中
	 * @return 
	 */
	public String queryTwoCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String catid = request.getParameter("catid");
		Map<Category, List<Category>> queryTwoCategory = service.queryTwoCategory(Integer.parseInt(catid));
		HttpSession session = request.getSession();
		session.setAttribute("twoCategoryMap", queryTwoCategory);
		return "r:/shenyongqiang/childindex.jsp";
	}
	/**
	 * 根据三级分类的id查询分页查询所有该分类下的所有商品
	 * @return 
	 */
	public String queryGoodsAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String catid = request.getParameter("catid");
		List<Brand> queryBraandByCatid = bService.queryBraandByCatid(Integer.parseInt(catid));
		PageBean<Goods> pagebean = new GoodsServlet().queryByCategoryAll(request, Integer.parseInt(catid));
		HttpSession session = request.getSession();
		session.setAttribute("brandlist", queryBraandByCatid);
		session.setAttribute("goodslist", pagebean.getBeanList());
		session.setAttribute("page",pagebean);
		return "r:/shenyongqiang/goodslist.jsp";
	}
	/**
	 * 查询所有的类别名称
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void quaryAllCaregoryName(HttpServletRequest req,HttpServletResponse res) throws IOException  {
		PrintWriter out = res.getWriter();
	    List<Category> listname = service.queryAllCategoryName();
	    String jsonstring=JSON.toJSONString(listname);
		JSONArray parseArray = JSONArray.parseArray(jsonstring);
		out.println(parseArray);
	}
	public void quaryNameToGoods(HttpServletRequest req,HttpServletResponse res) throws Exception  {
		PrintWriter out = res.getWriter();
	    String catname = req.getParameter("catname");
		String fileaddress = service.fileaddress(catname);
		System.out.println(fileaddress);
		HttpSession session = req.getSession();
		session.setAttribute("tip", fileaddress);
		/*String jsonstring=JSON.toJSONString(fileaddress);
		JSONArray parseArray = JSONArray.parseArray(jsonstring);
		out.println(parseArray);*/
		/*System.out.println(fileaddress);*/
		out.println("0");
	}


}
